package se.esss.stephenmolloy;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import py4j.GatewayServer;
import edu.stanford.slac.aaron.klystron.Klystron;

// The following is the entry class for the AJDisk klystron server.
// It works by adding a new entry to a list each time the remote user
// requests a new klystron.
public class AJDiskEntryPoint {

    private ArrayList klyList;
    private static ArrayList gatewayList;

    public AJDiskEntryPoint() {
	 klyList = new ArrayList(); 
	 printMessage("New connection to Gateway Server");
    }

    // Add a new klystron to the end of the list
    public void addNewKlystron() {
	    klyList.add(new Klystron());
	    //printMessage("New klystron created");
    }

    // Add a new klystron to the end of the list *and* return it
    public Klystron getNewKlystron() {
	    addNewKlystron();
	    return getKlystron(klyList.size()-1);
    }

    // Return the klystron at index i
    public Klystron getKlystron(int i) {
	    Klystron klys = (Klystron)klyList.get(i);
	    return klys;
    }

    // Empty the entire list
    // Note that this can be used to save memory, since any copies of list members stored
    // in the remote process will be unaffected by clearing the list.
    public void clearList() {
            for (int i=0; i<klyList.size(); i++) {
                Klystron a = (Klystron)klyList.get(i);
                a = null;
            }
	    klyList.clear();
        klyList = new ArrayList();
	    //printMessage("Klystron list cleared");
    }

    private void printMessage(String mess) {
	    Date date = new Date();
	    System.out.println(date.toString() + ": " + mess);
    }

    // Start the server
    public static void main(String[] args) {
	    gatewayList = new ArrayList();
	    if (args.length == 0) {
	            //System.out.println("Starting Gateway Server on default port");
                    gatewayList.add(new GatewayServer(new AJDiskEntryPoint()));
		    GatewayServer a = (GatewayServer)gatewayList.get(0);
		    a.start();
	            //System.out.println("Server started.");
	    }
	    if (args.length == 1) {
	            //System.out.println("Starting Gateway Server on port " + args[0]);
	            int portnum = Integer.parseInt(args[0]);
                    gatewayList.add( new GatewayServer(new AJDiskEntryPoint(), portnum) );
		    GatewayServer a = (GatewayServer)gatewayList.get(0);
		    a.start();
	            //System.out.println("Server started.");
	    }
	    if (args.length > 1) {
	            for (int i=0; i<args.length; i++) {
	        	//System.out.println("Starting Gateway Server on port " + args[i]);
	                int portnum = Integer.parseInt(args[i]);
                        gatewayList.add( new GatewayServer(new AJDiskEntryPoint(), portnum) );
		        GatewayServer a = (GatewayServer)gatewayList.get(i);
		        a.start();
	                //System.out.println("Server started.");
	            }
	    }
            //GatewayServer gatewayServer = new GatewayServer(new AJDiskEntryPoint());
            //gatewayServer.start();
            //System.out.println("Gateway Server Started");
    }

}
